﻿using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Reflection;
using IDAL;
using Model;

namespace BLL
{
    public class ProductType : BaseService<ProductTypeInfo>
    {
        private static readonly IProductType dal = DALFactory.DataAccess.CreateProductType();
        private static readonly IBLLStrategy.IProductTypeStrategy productTypeInsertStrategy = LoadInsertStrategy();

        public override void SetCurrentRepository()
        {
            CurrentRepository = dal;
        }

        public ProductTypeInfo GetProductTypebyName(string productTypeName)
        {
            return dal.GetProductTypebyName(productTypeName);
        }

        public IList<ProductTypeInfo> GetProductTypesbyCustomerID(string customerID)
        {
            if (string.IsNullOrEmpty(customerID))
                return null;
            return dal.GetProductTypesbyCustomerID(customerID);
        }

        public bool ProductType_Exist(string productTypeID)
        {
            return !string.IsNullOrEmpty(dal.LoadEntity(productTypeID).ProductTypeID);
        }

        private static IBLLStrategy.IProductTypeStrategy LoadInsertStrategy()
        {

            // Look up which strategy to use from config file
            string path = ConfigurationManager.AppSettings["ProductTypeStrategyAssembly"];
            string className = ConfigurationManager.AppSettings["ProductTypeStrategyClass"];

            // Using the evidence given in the config file load the appropriate assembly and class
            return (IBLLStrategy.IProductTypeStrategy)Assembly.Load(path).CreateInstance(className);
        }
    }
}
